<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0,user-scalable=no, viewport-fit=cover" />
    <link rel="icon" type="image/png" href="/favicon.png" />
    <meta name="renderer" content="webkit" />
    <meta name="force-rendering" content="webkit" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <title>登录中</title>
    <script src="/src/library/jquery/1.8.3/jquery.js"></script>
  </head>

  <body>
    <script>
      var __api_server__ = <%- apiServer %>;
      var getRequest = () => {
          var encodeUrl = new URL(location.href.replace('#', encodeURIComponent('#')));
          var search = encodeUrl.search.replace('?', '');
          var theRequest = new Object();
          var strs = search.split('&');
          for (var i = 0; i < strs.length; i++) {
              var result = strs[i].split('=');
              theRequest[result[0]] = decodeURIComponent(result[1]);
          }
          return theRequest;
      };
      var request = getRequest();
      var getGlobalMeta = cb => {
          $.ajax({
              url: __api_server__.main + 'Global/GetGlobalMeta',
              type: 'POST',
              data: JSON.stringify({}),
              dataType: 'JSON',
              contentType: 'application/json',
          }).done(res => {
              var data = res.data;
              var ApiUrl = data['md.global'].Config.ApiUrl
              cb(ApiUrl);
          })
      };
      getGlobalMeta((ApiUrl) => {
          $.ajax({
              url: ApiUrl + "/oauth2/h5login",
              type: 'POST',
              data: JSON.stringify(request),
              dataType: 'JSON',
              contentType: 'application/json',
          }).done(res => {
              if (res.success) {
                  var ua = navigator.userAgent;
                  var isIOS = !!ua.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
                  var info = {
                        sessonId: '',
                        type: 'native',
                        settings: {
                            action: 'login',
                            accessTokenInfo: res
                        }
                    }
                  var string = JSON.stringify(info);
                  var base64 = window.btoa(string);
                  if (isIOS) {
                      window.webkit.messageHandlers.MD_APP_REQUEST.postMessage(base64);
                  } else {
                      window.Android.MD_APP_REQUEST(base64);
                  }
              } else {
                  alert('登录失败!')
              }
          })
      })
    </script>
  </body>
</html>
